package com.cskaoyan.controller.wxcontroller;


import com.cskaoyan.bean.pojo.*;
import com.cskaoyan.bean.pojo.System;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.vo.wx.FloorGoodsVo;
import com.cskaoyan.bean.vo.wx.GrouponVo;
import com.cskaoyan.bean.vo.wx.HomeIndexVo;
import com.cskaoyan.beanwx.vo.UserIndexVo;
import com.cskaoyan.exception.DataDisplayException;
import com.cskaoyan.service.GoodsService;
import com.cskaoyan.service.OrderService;
import com.cskaoyan.service.PromotionService;
import com.cskaoyan.service.SystemService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;


/**
 * @author: lds
 * @clsss: UserIndexController
 */


@RestController
public class UserIndexController {

    @Autowired
    OrderService orderService;

    @Autowired
    GoodsService goodsService;

    @Autowired
    PromotionService promotionService;

    @Autowired
    SystemService systemService;

    @RequestMapping("wx/user/index")
    public BaseRespVo userIndex() throws DataDisplayException {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        //未登录user为null，需要判空
        if (user==null){
            return BaseRespVo.fail("请登录");
        }
        UserIndexVo userIndexVo = orderService.queryUserOrder(user.getId());
        if (userIndexVo == null) {
            throw new DataDisplayException();
        }

        return BaseRespVo.ok(userIndexVo);
    }




    @GetMapping("/wx/home/index")
    public BaseRespVo homeIndex() {
        List<Goods> newGoodsList = goodsService.queryNewGoodsList();
        List<Goods> hotGoodsList = goodsService.queryHotGoodsList();

        // 分类栏目商品显示数量 cskaoyan_mall_wx_catlog_goods
        System system = systemService.querySystemByKeyName("cskaoyan_mall_wx_catlog_goods");
        int value1 = 0;
        try {
            value1 = Integer.parseInt(system.getKeyValue());
        } catch (Exception ignore) {
        }
        List<FloorGoodsVo> floorGoodsList = goodsService.queryFloorGoodsList(value1);
        List<Brand> brandList = goodsService.queryGoodsBrand();
        List<Category> channel = goodsService.queryGoodsCategory();

        List<Ad> banner = promotionService.queryAllAds();

        List<Coupon> couponList = promotionService.queryAllCoupons();
        // days基于timeType变化，如果是0，days=days,如果是1，days=endTime - startTime
        for (Coupon coupon : couponList) {
            if (coupon.getTimeType() == 1) {
                coupon.setDays((short)((coupon.getEndTime().getTime() - java.lang.System.currentTimeMillis())/1000/60/60/24));
            }
            if (coupon.getDays() <= 0) {
                couponList.remove(coupon);
            }
        }

        List<GrouponVo> grouponList = promotionService.queryAllGroupons();
        List<Topic> topicList = promotionService.queryAllTopics();


        HomeIndexVo homeIndexVo = new HomeIndexVo();
        // 新品首发栏目商品显示数量key_name = cskaoyan_mall_wx_index_new
        System system2 = systemService.querySystemByKeyName("cskaoyan_mall_wx_index_new");
        int value2 = 0;
        try {
            value2 = Integer.parseInt(system2.getKeyValue());
        } catch (Exception ignore) {
        }
            homeIndexVo.setNewGoodsList(value2 == 0 || value2 >= newGoodsList.size() ? newGoodsList : newGoodsList.subList(0, value2));
        // 品牌制造商直供栏目品牌商显示数量key_name = cskaoyan_mall_wx_index_brand
        System system3 = systemService.querySystemByKeyName("cskaoyan_mall_wx_index_brand");
        int value3 = 0;
        try {
            value3 = Integer.parseInt(system3.getKeyValue());
        } catch (Exception ignore) {
        }
        homeIndexVo.setBrandList(value3 == 0 || value3 >= brandList.size() ? brandList : brandList.subList(0, value3));
        homeIndexVo.setBanner(banner);
        // 分类栏目显示数量 cskaoyan_mall_wx_catlog_list
        System system4 = systemService.querySystemByKeyName("cskaoyan_mall_wx_catlog_list");
        int value4 = 0;
        try {
            value4 = Integer.parseInt(system4.getKeyValue());
        } catch (Exception ignore) {
        }
        homeIndexVo.setChannel(value4 == 0 || value4 >= channel.size() ? channel : channel.subList(0, value4));
        homeIndexVo.setCouponList(couponList);
        homeIndexVo.setFloorGoodsList(floorGoodsList);
        homeIndexVo.setGrouponList(grouponList);
        // 人气推荐栏目商品数量 cakaoyan_mall_wx_index_hot
        System system5 = systemService.querySystemByKeyName("cakaoyan_mall_wx_index_hot");
        int value5 = 0;
        try {
            value5 = Integer.parseInt(system5.getKeyValue());
        } catch (Exception ignore) {
        }
        homeIndexVo.setHotGoodsList(value5 == 0 || value5 >= hotGoodsList.size() ? hotGoodsList : hotGoodsList.subList(0, value5));
        // 专题精选栏目显示数量 cskaoyan_mall_wx_index_topic
        System system6 = systemService.querySystemByKeyName("cskaoyan_mall_wx_index_topic");
        int value6 = 0;
        try {
            value6 = Integer.parseInt(system6.getKeyValue());
        } catch (Exception ignore) {
        }
        homeIndexVo.setTopicList(value6 == 0 || value6 >= topicList.size() ? topicList : topicList.subList(0, value6));
        return BaseRespVo.ok(homeIndexVo);
    }


}

